//
//  NSAttributedString+Extension.swift
//  UIKitExtentions
//
//  Created by Alean on 2023/3/20.
//

import Foundation

public extension NSMutableAttributedString {
    /// $26.40
    public static func rmbAttributedString(_ coin: String) -> NSMutableAttributedString? {
        let lik = coin.substring(to: 1)//$
        let otherCoi = coin.substring(from: 1)//26.40

        let rmbs = otherCoi.components(separatedBy: ".")
        let zrmb = rmbs.first ?? otherCoi
        let rmbStr = NSMutableAttributedString(elements: [
            (str: lik, attr: [NSAttributedString.Key.foregroundColor : UIColor.hexString("#FF3133"), NSAttributedString.Key.font : UIFont.systemFont(ofSize: 18, weight: .bold)]),
            (str: zrmb, attr: [NSAttributedString.Key.foregroundColor : UIColor.hexString("#FF3133"), NSAttributedString.Key.font : UIFont.systemFont(ofSize: 18, weight: .bold)]),
        ])

        if rmbs.count > 1 {
            let drmb = rmbs[1]
            if let ormbStr = NSMutableAttributedString(elements: [
                (str: ".", attr: [NSAttributedString.Key.foregroundColor : UIColor.hexString("#FF3133"), NSAttributedString.Key.font : UIFont.systemFont(ofSize: 20, weight: .bold)]),
                (str: drmb, attr: [NSAttributedString.Key.foregroundColor : UIColor.hexString("#FF3133"), NSAttributedString.Key.font : UIFont.systemFont(ofSize: 16, weight: .bold)]),
            ]) {
                rmbStr?.append(ormbStr)
            }
        }
        return rmbStr
    }

    /// 带删除线$26.40
    public static func oldRmbAttributedString(oldCoin: String) -> NSMutableAttributedString? {
        let oldrmbStr = NSMutableAttributedString(elements: [
            (str: oldCoin, attr: [
                NSAttributedString.Key.foregroundColor : UIColor.hexString("#D8D8D8"),
                NSAttributedString.Key.font : UIFont.systemFont(ofSize: 11, weight: .regular),
                NSAttributedString.Key.strikethroughStyle : NSUnderlineStyle.single.rawValue,
                NSAttributedString.Key.strikethroughColor : UIColor.hexString("#D8D8D8")
            ]),
        ])
        return oldrmbStr
    }

    public static func addElement(_ string: String, color: UIColor, font: UIFont) -> (str :String, attr : [NSAttributedString.Key : Any]) {
        return (str: string,
                attr: [
                    NSAttributedString.Key.foregroundColor : color,
                    NSAttributedString.Key.font : font
                ]
        )
    }

    public convenience init?(elements: [(str :String, attr : [NSAttributedString.Key : Any])]) {
        guard elements.count > 0 else {
            return nil
        }
        let allString:String = elements.reduce("") { (res, ele) -> String in
            return res + ele.str
        }
        self.init(string: allString)
        for ele in elements {
            let eleStr = ele.str
            let range:Range = allString.range(of: eleStr)!
            let nsRange:NSRange = NSRange(range, in: allString)
            self.addAttributes(ele.attr, range: nsRange)
        }
    }
    
    private static func checkLeftString(attrstring: NSMutableAttributedString, text: String, spec: Int = 0, value: (mainText: String, mainColor: UIColor, mainFont: UIFont, isMatch: Bool)) {
        let str = NSString(string: text)
        
        let theRange = str.range(of: value.0)
        //因为在原先富文本的基础上，所以需要加上之前的便宜
        let range = NSRange(location: theRange.location + spec, length: theRange.length)
        attrstring.addAttribute(NSAttributedString.Key.foregroundColor, value: value.1, range: range)
        attrstring.addAttribute(NSAttributedString.Key.font, value: value.2, range: range)
        
        //去掉前几位字符
        let leftString = text.dropFirst(theRange.location + theRange.length)
        if !leftString.isEmpty, leftString.contains(value.0) {
            checkLeftString(attrstring: attrstring, text: String(leftString), spec: (spec + theRange.location + theRange.length) ,value: value)
        } else {
            print("end")
        }
    }

    public static func attributedTextRangeMatch(_ text: String, color: UIColor, font: UIFont,
                                                others: [(mainText: String, mainColor: UIColor, mainFont: UIFont, isMatch: Bool)]? = nil) -> NSMutableAttributedString {
        let attrstring = NSMutableAttributedString(string: text,
                                                   attributes: [
                                                    NSAttributedString.Key.foregroundColor: color,
                                                    NSAttributedString.Key.font: font
                                                   ])
        if let others = others, !others.isEmpty {
            for (index, value) in others.enumerated() {
                if value.3 {//isMatch 表示是否一定要一致
                    checkLeftString(attrstring: attrstring, text: text, value: value)
                } else {
                    for character in value.0 {
                        checkLeftString(attrstring: attrstring, text: text, value: (String(character), value.1, value.2, value.3))
                    }
                }
            }
        }
        return attrstring
    }
}
